package com.bdc.memory.webservice.impl;

import com.bdc.BdcVar;
import com.bdc.memory.LoginException;
import com.bdc.memory.domain.RecordInfo;
import com.bdc.memory.domain.TagInfo;
import com.bdc.memory.service.IMemoryService;
import com.bdc.memory.service.IRecordService;
import com.bdc.memory.service.ITagService;
import com.bdc.memory.webservice.IBdcWebService;
import com.bdc.user.UserVar;
import com.bdc.user.service.IUserService;
import com.bdc.utils.StringUtils;
import org.apache.log4j.Logger;

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * User: shawn
 * Date: 2011-6-16
 * Time: 19:29:02
 */
@WebService(name = "bdc", portName = "bdc", serviceName = "bdc", targetNamespace = "bdc")
public class IBdcWebServiceImpl implements IBdcWebService {

    private static final Logger logger = Logger.getLogger(IBdcWebServiceImpl.class.getName());

    public List<RecordInfo> getTodayRecordList() {
        try {
            long userId = getUserId();
            IRecordService recordService = BdcVar.injector.getInstance(IRecordService.class);

            List<RecordInfo> recordList = recordService.getTodayRecordList(userId);
            for (RecordInfo recordInfo : recordList) {
                recordInfo.setMemory(StringUtils.toUtf8(recordInfo.getMemory()));
                recordInfo.setDescription(StringUtils.toUtf8(recordInfo.getDescription()));
                recordInfo.setTag(StringUtils.toUtf8(recordInfo.getTag()));
            }

            return recordList;
        }
        catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<RecordInfo>();
        }
    }

    public boolean updateMemory(long recordId, int score) {
        try {
            IMemoryService memoryService = BdcVar.injector.getInstance(IMemoryService.class);
            memoryService.updateMemory(recordId, score);
            return true;
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean addRecord(String memory, String description, String tag) {
        try {
            long userId = getUserId();
            IRecordService recordService = BdcVar.injector.getInstance(IRecordService.class);

            memory = StringUtils.fromUtf8(memory);
            description = StringUtils.fromUtf8(description);
            tag = StringUtils.fromUtf8(tag);

            recordService.addRecord(userId, memory, description, tag);
            return true;
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean updateRecord(long recordId, String memory, String description, String tag) {
        try {
            long userId = getUserId();
            IRecordService recordService = BdcVar.injector.getInstance(IRecordService.class);
            recordService.updateRecord(userId, recordId, memory, description, tag);
            return true;
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean delRecord(long recordId) {
        try {
            long userId = getUserId();
            IRecordService recordService = BdcVar.injector.getInstance(IRecordService.class);
            recordService.delRecord(recordId);
            return true;
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public long addTag(@WebParam(name = "tag") String tag) {
        try {
            long userId = getUserId();
            ITagService tagService = BdcVar.injector.getInstance(ITagService.class);

            tag = StringUtils.fromUtf8(tag);
            return tagService.addTag(userId, tag);
        }
        catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    public boolean updateTag(long tagId, String tag) {
        try {
            long userId = getUserId();
            ITagService tagService = BdcVar.injector.getInstance(ITagService.class);

            tag = StringUtils.fromUtf8(tag);
            tagService.updateTag(userId, tagId, tag);

            return true;
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean delTag(long tagId) {
        try {
            long userId = getUserId();
            ITagService tagService = BdcVar.injector.getInstance(ITagService.class);
            tagService.delTag(tagId);
            return true;
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public List<TagInfo> getTagList() {
        try {
            long userId = getUserId();
            ITagService tagService = BdcVar.injector.getInstance(ITagService.class);

            List<TagInfo> tagList = tagService.getTagList(userId);
            for (TagInfo tagInfo : tagList) {
                tagInfo.setTag(StringUtils.toUtf8(tagInfo.getTag()));
            }

            return tagList;
        }
        catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<TagInfo>();
        }
    }

    public List<RecordInfo> getTagRecordList(long tagId, long from, long count) {
        try {
            long userId = getUserId();
            IRecordService recordService = BdcVar.injector.getInstance(IRecordService.class);

            List<RecordInfo> recordList = recordService.getTagRecordList(tagId, from, count);
            for (RecordInfo recordInfo : recordList) {
                recordInfo.setMemory(StringUtils.toUtf8(recordInfo.getMemory()));
                recordInfo.setDescription(StringUtils.toUtf8(recordInfo.getDescription()));
                recordInfo.setTag(StringUtils.toUtf8(recordInfo.getTag()));
            }

            return recordList;
        }
        catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<RecordInfo>();
        }
    }

    public long login(String mail, String password) {
        try {
            IUserService userService = BdcVar.injector.getInstance(IUserService.class);
            HttpSession session = BdcVar.injector.getInstance(HttpSession.class);

            long result = userService.login(mail, password);

            boolean isLogined = result > 0;
            if (isLogined) {
                logger.debug("user [" + mail + "] logined");
                logger.debug("sessionId [" + session.getId() + "]");

                session.setAttribute(BdcVar.SESSION_USER_ID, result);
            }

            return result;
        }
        catch (Exception e) {
            e.printStackTrace();
            return UserVar.LOGIN_SERVER_ERROR;
        }
    }

    private long getUserId() throws LoginException {
        HttpSession session = BdcVar.injector.getInstance(HttpSession.class);
        logger.debug("sessionId [" + session.getId() + "]");
        boolean isLogined = session.getAttribute(BdcVar.SESSION_USER_ID) != null;
        if (!isLogined) throw new LoginException();
        return (Long) session.getAttribute(BdcVar.SESSION_USER_ID);
    }
}
